package com.bofa.utils;

import static com.bofa.test.cases.base.BOFASeleniumBase.baseProperties;

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;

public class EmailSender {
	static Logger logger = Logger.getLogger(EmailSender.class.getName());
	public void sendEmail() {
	      // Recipient's email ID needs to be mentioned.
	      String to = baseProperties.getProperty("email.to");
	      // Sender's email ID needs to be mentioned
	      String from = baseProperties.getProperty("email.from");
	      final String username = baseProperties.getProperty("email.username");
	      final String password = baseProperties.getProperty("email.password");

	      
	      String host = "smtp.gmail.com";
	      Properties props = new Properties();
	      props.put("mail.smtp.auth", "true");
	      props.put("mail.smtp.starttls.enable", "true");
	      props.put("mail.smtp.host", host);
	      props.put("mail.smtp.port", "587");

	      // Get the Session object.
	      Session session = Session.getInstance(props,
	      new javax.mail.Authenticator() {
	         protected PasswordAuthentication getPasswordAuthentication() {
	            return new PasswordAuthentication(username, password);
	         }
	      });

	      try {
	         // Create a default MimeMessage object.
	         Message message = new MimeMessage(session);

	         // Set From: header field of the header.
	         message.setFrom(new InternetAddress(from));

	         // Set To: header field of the header.
	         message.setRecipients(Message.RecipientType.TO,
	         InternetAddress.parse(to));

	         // Set Subject: header field
	         message.setSubject("Test suite executed sucessfully. Check the results");

	         // Now set the actual message
	         message.setText("Test suite executed sucessfully. Check the results");
	         
	         addAttachment(message);

	         // Send message
	         Transport.send(message);

	         System.out.println("Sent message successfully....");

	      } catch (MessagingException e) {
	    	  logger.error("Error in sending email"+ExceptionUtils.getStackTrace(e));
	          throw new RuntimeException(e);
	      }
	   }
	
	private void addAttachment(Message message) throws MessagingException{
		File fileAttachment = new File("D:\\Development\\workspaces\\verizon\\bank-of-america\\test-output\\emailable-report.html");
		
		// create the message part 
	    MimeBodyPart messageBodyPart = 
	      new MimeBodyPart();

	    //fill message
	    messageBodyPart.setText("Hi");

	    Multipart multipart = new MimeMultipart();
	    multipart.addBodyPart(messageBodyPart);

	    // Part two is attachment
	    messageBodyPart = new MimeBodyPart();
	    DataSource source = 
	      new FileDataSource(fileAttachment);
	    messageBodyPart.setDataHandler(
	      new DataHandler(source));
	    messageBodyPart.setFileName("bofa-test-results.html");
	    multipart.addBodyPart(messageBodyPart);

	    // Put parts in message
	    message.setContent(multipart);
	}
}
